package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.ReturnTuition;

@Repository
public interface ReturnTuitionDao extends JpaRepository<ReturnTuition, Integer> {

    @Transactional
    @Modifying
    Integer deleteByReturnId(Integer returnId);

    List<ReturnTuition> findByReturnId(Integer returnId);

    @Transactional
    @Modifying
    Integer deleteByReturnIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,rt.*,ct.typeName \r\n" +
            "FROM ((return_tuition rt INNER JOIN quit_class qc ON rt.quit_Id = qc.quit_Id ) \r\n" +
            "INNER JOIN users u ON u.user_Id = qc.user_Id )\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = rt.charge_type_Id")
    List<Map<String, Object>> queryAllStudentAndReturnInfo();

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,qc.originalClass,qc.quit_Id as quitId \r\n" +
            "FROM quit_class qc INNER JOIN users u ON qc.user_Id = u.user_Id\r\n" +
            "WHERE qc.quitStatus = '已批阅' \r\n" +
            "AND qc.quit_Id not in (SELECT quit_Id FROM return_tuition)")
    List<Map<String, Object>> queryAllQuitClassAndStudentInfo();

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,rt.* FROM (return_tuition rt INNER JOIN quit_class qc ON rt.quit_Id = qc.quit_Id )\r\n" +
            "INNER JOIN users u ON u.user_Id = qc.user_Id\r\n" +
            "WHERE rt.originalClass LIKE CONCAT('%',:originalClass,'%')")
    List<Map<String, Object>> findByOriginalClassLike(@Param("originalClass") String originalClass);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,rt.* FROM (return_tuition rt INNER JOIN quit_class qc ON rt.quit_Id = qc.quit_Id )\r\n" +
            "INNER JOIN users u ON u.user_Id = qc.user_Id\r\n" +
            "WHERE u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,rt.* FROM (return_tuition rt INNER JOIN quit_class qc ON rt.quit_Id = qc.quit_Id )\r\n" +
            "INNER JOIN users u ON u.user_Id = qc.user_Id\r\n" +
            "WHERE u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,rt.* FROM (return_tuition rt INNER JOIN quit_class qc ON rt.quit_Id = qc.quit_Id )\r\n" +
            "INNER JOIN users u ON u.user_Id = qc.user_Id\r\n" +
            "WHERE rt.sumMoney LIKE CONCAT('%',:sumMoney,'%')")
    List<Map<String, Object>> findBySumMoneyLike(@Param("sumMoney") String sumMoney);

    @Query(nativeQuery = true, value = "SELECT u.user_Id,u.account as userName,u.nickName,rt.*,ct.typeName \r\n" +
            "FROM ((return_tuition rt INNER JOIN quit_class qc ON rt.quit_Id = qc.quit_Id )\r\n" +
            "INNER JOIN users u ON u.user_Id = qc.user_Id ) \r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = rt.charge_type_Id\r\n" +
            "WHERE u.user_Id = :userId")
    List<Map<String, Object>> selectQuitTuitionByUserId(@Param("userId") Integer userId);

}
